<?php
	include_once("king_head.php");
?>

<CENTER>
<H2> Reseller Management - New Account</H2>
<TABLE border=1 width='90%'>
	<TR>
		<TH>S.No.</TH>
		<TH>Name & email</TH>
		<TH>Contact #</TH>
		<TH>Address</TH>
		<TH>Country</TH>
		<TH>Reg. Date</TH>
		<TH>Joining Date</TH>
		<TH>Account type</TH>
		<TH>Allow/<BR>Activate</TH>
		<TH>Click to Remove</TH>
	</TR>
<?php
	include_once("ClsSebent/config.php");
	$action=$_REQUEST['action'];
	$subs_type=$_REQUEST['subs_type'];
	$uid=$_REQUEST['uid'];

	if($action=="Activate"){
		$sql="SELECT * FROM site_home WHERE id='101'";
		$result = mysql_query($sql);
		$num=1;
		if($main_data=mysql_fetch_array($result)){
			$sql="INSERT INTO `site_home` (`logo` , `logo_alt` , `company_name` , `company_name_img` , `company_name_img_alt` , `slogan` , `contact_info` , `copyright` , `page_bg_image` , `page_bg_color` , `page_text_color` , `page_link_text_color` , `page_link_visited_color` , `page_link_rollover_color` , `menu_link_text_color` , `menu_link_visited_color` , `menu_link_rollover_color` , `meta_keywords` , `meta_description` , `meta_title` , `paypal_id` , `payment_ins` , `product_profit_pct` , `product_tax_pct` , `product_shipping_amt` , `sync_logo` , `sync_navbar` , `sync_contact_info` , `sync_skin` , `sync_paypal` , `sync_pricing` , `sync_meta` , `sync_all`, page_title_color ) VALUES ('" . mysql_escape_string ($main_data["logo"]) . "', '" . mysql_escape_string($main_data["logo_alt"]) . "', '" .mysql_escape_string($main_data["company_name"]) . "', '" . mysql_escape_string($main_data["company_name_img"]) . "', '" . mysql_escape_string($main_data["company_name_img_alt"]) . "', '" . mysql_escape_string($main_data["slogan"]) . "', '" . mysql_escape_string($main_data["contact_info"]) . "', '" . mysql_escape_string($main_data["copyright"]) . "', '" . mysql_escape_string($main_data["page_bg_image"]) . "', '" . $main_data["page_bg_color"] . "', '" . $main_data["page_text_color"] . "', '" . $main_data["page_link_text_color"] . "', '" . $main_data["page_link_visited_color"] . "', '" . $main_data["page_link_rollover_color"] . "', '" . $main_data["menu_link_text_color"] . "', '" . $main_data["menu_link_visited_color"] . "', '" . $main_data["menu_link_rollover_color"] . "', '" . mysql_escape_string($main_data["meta_keywords"]) . "', '" . mysql_escape_string($main_data["meta_description"]) . "', '" . mysql_escape_string($main_data["meta_title"]) . "', '" . $main_data["paypal_id"] . "', '" . mysql_escape_string($main_data["payment_ins"]) . "', '" . $main_data["product_profit_pct"] . "', '" . $main_data["product_tax_pct"] . "', '" . $main_data["product_shipping_amt"] . "', '" . $main_data["sync_logo"] . "', '" . $main_data["sync_navbar"] . "', '" . mysql_escape_string($main_data["sync_contact_info"]) . "', '" . $main_data["sync_skin"] . "', '" . $main_data["sync_paypal"] . "', '" . $main_data["sync_pricing"] . "', '" . $main_data["sync_meta"] . "', '" . $main_data["sync_all"] . "', '" . $main_data["page_text_color"] . "')";
			//echo '***INSERT QRY: '.$sql;
			/*$Inres=*/mysql_query($sql);
			//echo "Result Id: ".$Inres;//exit;					
			$new_site_id=mysql_insert_id();
//echo "<BR>*******New site Id : ".$new_site_id."******<BR>";
			$c_n_i=$main_data["company_name_img"];
			if($c_n_i != "") copy("./photos/${site_id}_" .$c_n_i,"./photos/${new_site_id}_" . $c_n_i);// 

			$logo=$main_data["logo"];
			if($logo != "") copy("./logos/${site_id}_" ."frontlogo.jpg" ,"./logos/${new_site_id}_" . $logo);//$logo

			$b_bg_i=$main_data["page_bg_image"];
			if("$b_bg_i" !="") copy("./photos/header/${site_id}_" . $b_bg_i,"./photos/header/${new_site_id}_" . $b_bg_i);
		}
		
		$date=date("Y-m-d");
		if("$subs_type"=="M") $end_dt = strftime("%Y-%m-%d", strtotime($join_dt) + mktime(0, 0, 0, 2, 1, 1970));
		if("$subs_type"=="Q") $end_dt = strftime("%Y-%m-%d", strtotime($join_dt) + mktime(0, 0, 0, 4, 4, 1970));
		if("$subs_type"=="S") $end_dt = strftime("%Y-%m-%d", strtotime($join_dt) + mktime(0, 0, 0, 7, 5, 1970));
		if("$subs_type"=="A") $end_dt = strftime("%Y-%m-%d", strtotime($join_dt) + mktime(0, 0, 0, 1, 1, 1971));

		$sql="UPDATE reseller SET active='Y', subs_type='".$subs_type."', join_dt='".$join_dt."', end_dt='".$end_dt."', site_id=".$new_site_id." WHERE uid='".$uid."'";
		//echo $sql;
		mysql_query($sql);

		$sql="INSERT INTO subs_detail (uid, start_dt, end_dt, renewal_dt) values('$uid', '$join_dt', '$end_dt', '$date')";
		//echo $sql;
		mysql_query($sql);

		$new_nav_res = mysql_query("SELECT MAX(id) FROM site_navbar");
		$new_nav_row = mysql_fetch_row($new_nav_res);
		$new_navbar_base=$new_nav_row[0] + 1;

$rsql="Select * from site_page where site_id=101";
$rres=mysql_query($rsql);
		while($rrow=mysql_fetch_array($rres))
		{
			$Iqry="INSERT INTO site_page (site_id, page_title, page_content, page_content_img, page_content_img_alt, page_content_ic, banner, banner_link, meta_keywords, meta_description, meta_title, admin_pg) VALUES ('$new_site_id','" . mysql_escape_string($rrow["page_title"]) . "','" . mysql_escape_string($rrow["page_content"]) ."', '" . $rrow["page_content_img"] . "', '" . $rrow["page_content_img_alt"] . "', 'C', '" . $rrow["banner"] . "', '" . $rrow["banner_link"] . "', '" . $rrow["meta_keywords"] ."', '" . $rrow["meta_description"] . "', '" . $rrow["meta_title"] . "', 'Y')";
			//echo $Iqry;
			mysql_query($Iqry);
		}
		//Pages Inserted.

$nsql="select * from site_navbar where site_id=101 ORDER BY link_position, link_text ";
$nres=mysql_query($nsql);
while($nrow=mysql_fetch_array($nres))
	{
		$page_title=mysql_escape_string($nrow["link_page_title"]);
		$link_text=mysql_escape_string($nrow["link_text"]);
		$link_page_title=mysql_escape_string($nrow["link_page_title"]);
		$link_parent=$nrow["link_parent"];
		$link_position=$nrow["link_position"];
		$new_navbar_id=$new_navbar_base;		
		$new_link_parent=0;
		//GET Link Parent Id.	
			
		if($link_parent!=0)
		{
		$pqry="select * from site_navbar where id=$link_parent";
		$pres=mysql_query($pqry);
		
		if($prow=mysql_fetch_array($pres))
		$parent_title=mysql_escape_string($prow["link_page_title"]);
		
		if($parent_title!="")
		{
		$piqry="select * from site_navbar where site_id='$new_site_id' and link_page_title='$parent_title'";
		//echo $piqry;
		$pires=mysql_query($piqry);
		if($pirow=mysql_fetch_array($pires))		
		$new_link_parent=mysql_escape_string($pirow["id"]);		
		}
		}
		//GET Link Page Id.
		$nqry="select * from site_page where site_id='$new_site_id' and page_title='$page_title'";		
//		echo $nqry;
		$nvres=mysql_query($nqry);
		if($nvrow=mysql_fetch_array($nvres))
		$new_link_page=$nvrow["id"];
		//echo "<BR>".$new_link_page;
		$Inqry="INSERT INTO site_navbar VALUES($new_navbar_id, $new_site_id, '$link_text', $new_link_page, '$page_title', $new_link_parent, '$link_position','Y', 'Y')";
		//echo $Inqry;
		mysql_query($Inqry);
		$new_navbar_base++;
	}//Links Inserted
	}//IF ACTION = ACTIVATE.
	
	if($action=="del")
	{
		$sql="DELETE FROM reseller WHERE uid='$uid'";
		//echo $sql;
		mysql_query($sql);
	}
	$sql="SELECT * FROM reseller WHERE active='N' AND site_id=0 ORDER BY reg_dt";
	$result = mysql_query($sql);
	$num=1;
	while($row=mysql_fetch_array($result))
	{
		$uid=addslashes($row["uid"]);
		$fname=addslashes($row["fname"]);
		$email=addslashes($row["email"]);
		$contact_no=$row["phone_no"];
		$mobile_no=$row["mobile_no"];
		$address=addslashes($row["address"]);
		$city=$row["city"];
		$state=$row["province"];
		$country=$row["country"];
		$zip=$row["zip"];
		$reg_dt=$row["reg_dt"];
		$subs_type=$row["subs_type"];
		echo "<tr>";
		echo "<td> <Form name=frm".$num."><input type=hidden name=site_id value=".$_SESSION['SITE_ID']."><input type=hidden name=uid value=$uid>" . $num . " </td>";
		echo "<td nowrap> $fname <BR> <a href='mailto:" . $email . "'>$email</a> </td>";
		echo "<td nowrap> Phone: $contact_no <BR> Mobile: $mobile_no </td>";
		echo "<td width='20%'> $address <BR> $city $state $zip </td>";
		echo "<td nowrap> $country </td>";
		echo "<td nowrap> $reg_dt </td>";
		echo "<td><input name=join_dt size=8 value='" . date("Y-m-d") . "'></td>";
		echo "<td><select name=subs_type>
			<option value=M>Monthly
			<option value=Q>Quarterly
			<option value=S>Semi-Annual
			<option value=A>Annual";
		echo "</select></td>";

		if($link_active == "Y") $set="checked";
		else $set="";
		echo "<td><input type=submit name=action value=Activate></td>";
		echo "<td><a href=reseller.php?action=del&site_id=".$_SESSION['SITE_ID']."&uid=" . $uid . ">Delete</a></td>";
		echo "</tr></form>";
		$num=$num+1;
	}
?>
<input type=hidden name=num value="<?php echo $num; ?>">
</TABLE>
</CENTER>

<?php
	include_once("king_foot.php");
	/*DELETED CODE : AFTER LINKS INSERTED.
		$sql="SELECT * FROM site_navbar WHERE site_id='101' AND link_active='Y' ORDER BY  id";

		$nav_res = mysql_query($sql);
		while($nav_row=mysql_fetch_array($nav_res)){
			$navbar_id=$nav_row["id"];
			$link_text=$nav_row["link_text"];
			$link_page=$nav_row["link_page"];
			$link_parent=$nav_row["link_parent"];
			$link_position=$nav_row["link_position"];
				
			if($link_page>100){
				$sql="SELECT * FROM site_page WHERE id='$link_page'";
				$page_res = mysql_query($sql);
				$page_row=mysql_fetch_array($page_res);
				$sql="INSERT INTO site_page (site_id, page_title, page_content, page_content_img, page_content_img_alt, page_content_ic, banner, banner_link, meta_keywords, meta_description, meta_title) VALUES ('$new_site_id','" . mysql_escape_string($page_row["page_title"]) . "','" . mysql_escape_string($page_row["page_content"]) ."', '" . $page_row["page_content_img"] . "', '" . $page_row["page_content_img_alt"] . "', 'C', '" . $page_row["banner"] . "', '" . $page_row["banner_link"] . "', '" . $page_row["meta_keywords"] ."', '" . $page_row["meta_description"] . "', '" . $page_row["meta_title"] . "')";
				//echo "<BR>Page : $sql";
				if(mysql_query($sql)){
					$new_link_page=mysql_insert_id();

					if ($page_row["page_content_img"] != "") {
						if(!copy("./photos/contents/101_" . $page_row["page_content_img"], "./photos/contents/${new_site_id}_" . $page_row["page_content_img"])){ echo "coping error!"; }
					}

					if ($page_row["banner"]!= "") {
							if(!copy("./photos/banners/101_" . $page_row["banner"],"./photos/banners/${new_site_id}_" . $page_row["banner"])){ echo "coping error!"; }
					}
				}

			
			}//if linkpage>100
			else $new_link_page =$link_page;		
			
			$new_navbar_id = $navbar_id   + $new_navbar_base - 101;
			if($link_parent>100) $new_link_parent=$link_parent + $new_navbar_base - 101;
			else $new_link_parent =$link_parent;

			$sql="INSERT INTO site_navbar VALUES('$new_navbar_id','$new_site_id', '$link_text', '$new_link_page', '$new_link_parent', '$link_position','Y')";
			mysql_query($sql);
			//echo "<BR>$new_navbar_id  $navbar_id  $new_navbar_base <BR>NAVBAR: ". $sql;
		}//while nav_row..*/
?>